package com.jiangxiaofei.citycloud.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.RealUserBean;
import com.jiangxiaofei.citycloud.bean.UserInfoBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.param.AddRealidParam;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/8 0008.
 */

public class MeRealInfoActivity extends BaseActivity implements MainConstant.AddUserRealInfoView
        , MainConstant.GetRealUserInfoView {

    @InjectView(R.id.user_real_name)
    EditText userRealName;
    @InjectView(R.id.user_real_rb_man)
    RadioButton userRealRbMan;
    @InjectView(R.id.user_real_rb_woman)
    RadioButton userRealRbWoman;
    @InjectView(R.id.user_real_rg_sex)
    RadioGroup userRealRgSex;
    @InjectView(R.id.user_real_age)
    EditText userRealAge;
    @InjectView(R.id.user_real_card_number)
    EditText userRealCardNumber;
    @InjectView(R.id.user_real_family_regist)
    EditText userRealFamilyRegist;
    @InjectView(R.id.user_real_info_post)
    Button userRealInfoPost;
    MainConstant.AdduserRealInfoPersenter adduserRealInfoPersenter;
    MainConstant.GetRealUserInfoPersenter getRealUserInfoPersenter;
    AddRealidParam param = new AddRealidParam();
    private RealUserBean realUserBean;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActivity = this;
        mContext = this;
        setContentView(R.layout.activity_me_real_info_layout);
        initTitle();
        ButterKnife.inject(this);
        adduserRealInfoPersenter = new MainPersenter(mContext, (MainConstant.AddUserRealInfoView) mActivity);
        getRealUserInfoPersenter = new MainPersenter(mContext, (MainConstant.GetRealUserInfoView) mActivity);
        UserInfoBean userInfo = Constants.userInfo;
        if (EmptyUtils.isNotEmpty(userInfo)) {
            if (Integer.valueOf(userInfo.getRealid()) == 1) {
                getRealUserInfoPersenter.getRealUserInfo();
            }
        }
    }

    public boolean isForm() {
        if (EmptyUtils.isEmpty(userRealName.getText().toString())) {
            toastText("请输入真实姓名");
            return false;
        }
        if (EmptyUtils.isEmpty(userRealAge.getText().toString())) {
            toastText("请输入年龄");
            return false;
        }
        if (EmptyUtils.isEmpty(userRealCardNumber.getText().toString())) {
            toastText("请输入身份证号码");
            return false;
        }
        if (EmptyUtils.isEmpty(userRealFamilyRegist.getText().toString())) {
            toastText("请输入户籍所在地");
            return false;
        }
        if (!RegexUtils.isIDCard18(userRealCardNumber.getText().toString())) {
            toastText("请输入正确的身份证号");
            return false;
        }
        return true;
    }

    private void toastText(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.user_real_info_post})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.user_real_info_post:
                //用户真实姓名提交按钮
                if (isForm()) {
                    setParam();
                    adduserRealInfoPersenter.addUserRealInfo();
                }
                break;
            default:
                break;
        }
    }

    //设置提交参数列表
    public void setParam() {
        param.setRealName(userRealName.getText().toString());
        param.setAge(Integer.valueOf(userRealAge.getText().toString()));
        param.setPlace(userRealFamilyRegist.getText().toString());
        param.setUserId(Constants.userId);
        param.setRealNumber(userRealCardNumber.getText().toString());
        if (userRealRbMan.isChecked()) {
            param.setAge(1);
        } else {
            param.setAge(0);
        }
    }

    @Override
    protected void setListener() {
        setCTitle("用户信息");
        setLeftOneBg(R.drawable.ic_back);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void setData(Object o) {
        toastText("提交成功");
        Constants.userInfo.setRealid("1");
        finish();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public AddRealidParam addUserRealInfo() {
        return param;
    }

    @Override
    public int getRealUserInfo() {
        return Constants.userId;
    }

    @Override
    public void setUserRealInfo(Object o) {
        List<RealUserBean> realUserList = (List<RealUserBean>) o;
        realUserBean = realUserList.get(0);
        userRealName.setText(realUserBean.getRealname());
        userRealName.setEnabled(false);
        userRealAge.setText(realUserBean.getAge());
        userRealAge.setEnabled(false);
        userRealCardNumber.setText(realUserBean.getIdnumber());
        userRealCardNumber.setEnabled(false);
        userRealFamilyRegist.setText(realUserBean.getPlace());
        userRealFamilyRegist.setEnabled(false);
        userRealRgSex.setEnabled(false);
        if (realUserBean.getSex().equals("0")) {
            userRealRbMan.setChecked(true);
            userRealRbWoman.setEnabled(false);
        } else {
            userRealRbMan.setEnabled(false);
            userRealRbWoman.setChecked(true);
        }
        userRealInfoPost.setText("确定");
        userRealInfoPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.finish();
            }
        });
    }

}
